package fun.flyee.android.pengshan.fgts;


import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.ConsoleMessage;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;

import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import fun.flyee.android.pengshan.Constant;
import fun.flyee.pengshan.social.R;
import fun.flyee.android.pengshan.events.WebEvent;
import fun.flyee.android.pengshan.utils.Logger;
import fun.flyee.android.pengshan.widgets.ExWebView;

public class LinkFragment extends BaseFragment {

    @BindView(R.id.web_view)
    public ExWebView mWebView;

    private String url;

    public LinkFragment() {

    }

    public static LinkFragment newInstance(String url) {
        LinkFragment fragment = new LinkFragment();
        fragment.url = url;
        return fragment;
    }

    protected int layoutId() {
        return R.layout.fgt_web;
    }

    public void onBackPressed() {
        if (mWebView != null) {
            if (mWebView.canGoBack()) {
                mWebView.goBack();
            } else {
                getActivity().finish();
            }
        }
    }

    @Override
    protected void initView() {
        mWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        mWebView.clearCache(true);
        mWebView.setBackgroundColor(Color.WHITE);
        mWebView.requestFocus();
        mWebView.setClickable(true);
        mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        mWebView.getSettings().setAppCacheEnabled(true);
        mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        mWebView.getSettings().setUseWideViewPort(true);
        mWebView.getSettings().setAllowFileAccess(true);
        mWebView.getSettings().setAllowContentAccess(true);
        mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        mWebView.getSettings().setLoadWithOverviewMode(true);
        mWebView.setGeolocationEnabled(false);
        mWebView.setMixedContentAllowed(true);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.setCookiesEnabled(true);
        mWebView.setThirdPartyCookiesEnabled(true);
        mWebView.addHttpHeader("X-Requested-With", "");
        mWebView.setListener(getActivity(), new ExWebView.Listener() {
            @Override
            public void onPageStarted(String url, Bitmap favicon) {
                Logger.i("start load : " + url + "   ");
            }

            @Override
            public void onPageFinished(String url) {
                Logger.i("finish load : " + url + "   ");
            }

            @Override
            public void onPageError(int errorCode, String description, String failingUrl) {
                Logger.e(errorCode + ":" + description + " - " + failingUrl);
            }

            @Override
            public void onDownloadRequested(String url, String suggestedFilename, String mimeType, long contentLength, String contentDisposition, String userAgent) {
                Logger.i("webview download : " + url + "   ");
            }

            @Override
            public void onExternalPageRequest(String url) {

            }

            @Override
            public void onViewSizeChanged(int nw, int nh, int ow, int oh) {

            }
        }, Constant.INTENT_REQUEST_PHOTO_CHOOSE);
        mWebView.setWebChromeClient(new WebChromeClient() {
            public boolean onConsoleMessage(ConsoleMessage cm) {
                Logger.i("Console", cm.message() + " -- From line "
                        + cm.lineNumber() + " of "
                        + cm.sourceId());
                return true;
            }
        });
        mWebView.loadUrl(url);
    }

    @Override
    protected void initData() {
    }

    @Override
    public void onDestroy() {
        if (mWebView != null) {
            try {
                if (mWebView.getParent() != null) {
                    ((ViewGroup) mWebView.getParent()).removeView(mWebView);
                }
                mWebView.setVisibility(View.GONE);
                mWebView.removeAllViews();
                mWebView.loadUrl("about:blank");
                mWebView.freeMemory();
                if (mWebView.isActivated()) mWebView.destroy();
            } catch (IllegalArgumentException e) {
                Logger.e(e.getMessage());
            }
            mWebView = null;
        }
        super.onDestroy();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {

    }

    @Subscribe
    public void whenWebEvent(WebEvent event) {

    }

    @Override
    public void setPath(String path) {
        this.url = path;
    }
}
